package com.android.lovingpet.common.userlist;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.android.lovingpet.R;
import com.android.lovingpet.common.userinfo.UserInfoActivity;
import com.android.lovingpet.bean.Account;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.ViewHolder> {

    private List<Account> accounts;
    private Context context;

    public UserListAdapter() {
        accounts = new ArrayList<>();
    }

    public UserListAdapter(List<Account> accounts) {
        this.accounts = accounts;
    }

    public void set(List<Account> accounts) {
        this.accounts = accounts;
    }

    public void add(List<Account> accounts) {
        this.accounts.addAll(accounts);
    }

    public void add(Account account) {
        accounts.add(account);
    }

    public void clear() {
        accounts.clear();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_user, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("account", accounts.get(holder.getAdapterPosition()));
                context.startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Account account = accounts.get(position);
        Glide.with(context).load(account.getHead()).into(holder.ivHead);
        holder.tvName.setText(account.getName());
        holder.tvSynopsis.setText(account.getSynopsis());
    }

    @Override
    public int getItemCount() {
        return accounts.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        CircleImageView ivHead;
        TextView tvName;
        TextView tvSynopsis;

        public ViewHolder(View itemView) {
            super(itemView);
            ivHead = itemView.findViewById(R.id.iv_head);
            tvName = itemView.findViewById(R.id.tv_name);
            tvSynopsis = itemView.findViewById(R.id.tv_variety);
        }
    }
}
